local width = 16
local depth = 16
local height = 4
local returnTurtle = true
local removeObstacles = true
local directionUp = 0
local directionDown = 1
local roomDirection = 0
local w = 0
local d = 0
local h = 0
local fuelSlot = 1
local refuelingTurtle = "Low fuel detected, refueling"
local outOfFuel = "Turtle out of fuel, waiting for refuel"
local turtleReturning = "Turtle returning to start"
local turtleReturned = "Turtle returned to start position"
local obstacleEncountered = "Obstacle encountered, waiting"
local roomCompleted = "Room digging completed"
local author = "Kevin Scroggins"
local nickname = "nitro glycerine"
local email = "nitro404@gmail.com
local website = "http://www.nitro404.com"

function checkFuel()
  if turtle.getItemCount(fuelSlot) == 0 then
    print(outOfFuel)
    
    while turtle.getItemCount(fuelSlot) == 0 do
      os.sleep(1)
    end
  end
  
  if turtle.getFuelLevel() < 15 then
    print(refuelingTurtle)
    turtle.select(fuelSlot)
    if turtle.refuel(1) ~= true then
      print(outOfFuel)
      
      while turtle.refuel(1) ~= true do
        os.sleep(1)
      end
    end
    
    checkFuel()
  end
end

function forward(d)
  if d == nil then
    d = 1
  elseif d < 1 then
    return
  end
  
  for i = 0, d - 1, 1 do
    checkFuel()
    
    if turtle.forward() ~= true then
      print(obstacleEncountered)
      
      while turtle.forward() ~= true do
        checkFuel()
        
        if removeObstacles == true then
          turtle.dig()
        end
        
        os.sleep(0.5)
      end
    end
  end
  
  checkFuel()
end

function back(d)
  if d == nil then
    d = 1
  elseif d < 1 then
    return
  end
  
  for i = 0, d - 1, 1 do
    checkFuel()
    
    if turtle.back() ~= true then
      print(obstacleEncountered)
      
      while turtle.back() ~= true do
        checkFuel()
        
        os.sleep(0.5)
      end
    end
  end
  
  checkFuel()
end

function up(d)
  if d == nil then
    d = 1
  elseif d < 1 then
    return
  end
  
  for i = 0, d - 1, 1 do
    checkFuel()
    
    if turtle.up() ~= true then
      print(obstacleEncountered)
      
      while turtle.up() ~= true do
        checkFuel()
        
        if removeObstacles == true then
          turtle.digUp()
        end
        
        os.sleep(0.5)
      end
    end
  end
  
  checkFuel()
end

function down(d)
  if d == nil then
    d = 1
  elseif d < 1 then
    return
  end
  
  for i = 0, d - 1, 1 do
    checkFuel()
    
    if turtle.down() ~= true then
      print(obstacleEncountered)
      
      while turtle.down() ~= true do
        checkFuel()
        
        if removeObstacles == true then
          turtle.digDown()
        end
        os.sleep(0.5)
      end
    end
  end
  
  checkFuel()
end

function left(n)
  if n == nil then
    n = 1
  elseif n < 1 then
    return
  end
  
  for i = 0, n - 1, 1 do
    turtle.turnLeft()
  end
end

function right(n)
  if n == nil then
    n = 1
  elseif n < 1 then
    return
  end
  
  for i = 0, n - 1, 1 do
    turtle.turnRight()
  end
end

function mineForward()
  checkFuel()
  
  turtle.dig()
  
  if turtle.forward() ~= true then
    while turtle.forward() ~= true do
      checkFuel()
      
      turtle.dig()
      
      os.sleep(0.5)
    end
  end
end

function mineUp()
  checkFuel()
  
  turtle.digUp()
  
  if turtle.up() ~= true then
    while turtle.up() ~= true do
      checkFuel()
      
      turtle.digUp()
      
      os.sleep(0.5)
    end
  end
end

function mineDown()
  checkFuel()
  
  turtle.digDown()
  
  if turtle.down() ~= true then
    while turtle.down() ~= true do
      checkFuel()
      
      turtle.digDown()
      
      os.sleep(0.5)
    end
  end
end

function mineFloor()
  local direction = 0
  
  for i = 0, width - 1, 1 do
    for j = 0, depth - 2, 1 do
      mineForward()
      
      if direction == 0 then
        d = d + 1
      elseif direction == 1 then
        d = d - 1
      end
    end
    
    if w < width - 1 then
      if direction == 0 then
        turtle.turnRight()
        mineForward()
        turtle.turnRight()
        
        direction = 1
      elseif direction == 1 then
        turtle.turnLeft()
        mineForward()
        turtle.turnLeft()
        
        direction = 0
      end
    end
    
    w = w + 1
  end
  
  if d > 0 then
    turtle.turnRight()
    turtle.turnRight()
    
    while d > 0 do
      forward()
      
      d = d - 1
    end
  end
  
  d = 0
  
  if w > 0 then
    turtle.turnRight()
    
    while w > 1 do
      forward()
      
      w = w - 1
    end
  end
  
  w = 0
  
  turtle.turnRight()
end

function mineRoom()
  print("Digging room with dimensions:")
  write("Width:  ")
  write(width)
  write("\n")
  write("Depth:  ")
  write(depth)
  write("\n")
  write("Height: ")
  write(height)
  write("\n")
  
  mineForward()
  
  for k = 0, height - 1, 1 do
    mineFloor()
    
    write("Floor ")
    write(k + 1)
    write(" completed!\n")
    
    if h < height - 1 then
      if roomDirection == directionUp then
        mineUp()
        
        h = h + 1
      elseif roomDirection == directionDown then
        mineDown()
        
        h = h - 1
      end
    end
  end
  
  if returnTurtle == true then
    if roomDirection == directionUp then
      while h > 0 do
        mineDown()
        
        h = h - 1
      end
    elseif roomDirection == directionDown then
      while h < 0 do
        mineUp()
        
        h = h + 1
      end
    end
    
    h = 0
    
    back()
    
    print(turtleReturned)
  end
  
  print(roomCompleted)
end

mineRoom()
